import { message } from 'antd';
import * as Api from './service';
import { DOMAIN } from '../../config/constant/constant';


export default {
  namespace: 'settlement',
  state: {
    merchantLst: [],
    reconciliateLst: [],
  },
  reducers: {
    updateReconciliateLst(state, { payload: resData }) {
      return { ...state, reconciliateLst: resData };
    },
    updateMerchant(state, { payload: resData }) {
      return { ...state, merchantLst: resData };
    },
    clear() {
      return {};
    },
  },
  effects: {
    *getReconciliateLst({ payload }, { call, put }) {
      const { data } = yield call(Api.getReconciliateLst, payload);
      yield put({ type: 'updateReconciliateLst', payload: data.data || [] });
    },
    *getMerchantLst({ payload }, { call, put }) {
      const { data } = yield call(Api.getMerchantLst, payload);
      yield put({ type: 'updateMerchant', payload: data.data || [] });
    },
    *getMechanismLst({ payload }, { call, put }) {
      const { data } = yield call(Api.getMechanismLst, payload);
      yield put({ type: 'updateReconciliateLst', payload: data.data || [] });
    },
    *getDownloadFileNm({ payload }, { call }) {
      const { data } = yield call(Api.getDownloadFileNm, payload);
      if (data.rspCd == '00000' && data.data) {
        const a = document.createElement('a'); // eslint-disable-line
        a.href = `/download?fileName=${data.data}`;
        a.click();
        message.destroy();
        message.success('下载成功');
      }
    },
    *getMechanismDownloadFileNm({ payload }, { call }) {
      const { data } = yield call(Api.getMechanismDownloadFileNm, payload);
      if (data.rspCd == '00000' && data.data) {
        const a = document.createElement('a'); // eslint-disable-line
        a.href = `/download?fileName=${data.data}`;
        a.click();
        message.destroy();
        message.success('下载成功');
      }
    },
  },
};
